package config.validator;

import com.jfinal.core.Controller;
import com.jfinal.validate.Validator;
import model.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;

/**
 * Created by Administrator on 2017/1/17.
 */
public class LoginValidator extends Validator{

    protected void validate(Controller c) {
        /*String error = null;
        String rememberMe = "true";
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(c.getPara("user.name"),c.getPara("user.pass"));
        if(rememberMe != null && "true".equals(rememberMe))
            token.setRememberMe(true);    // 记住我
        try {
            subject.login(token);
        } catch (UnknownAccountException | IncorrectCredentialsException e1) {
            error = "用户名或密码错误";
        }catch(ExcessiveAttemptsException e){
            //userService.lockAccountByNo(no);
            System.out.println("锁定账户");// 锁定账户
            error = "超过了尝试登录的次数，您的账户已经被锁定。";
        }catch (AuthenticationException e) {    // 其他错误
            if(e.getMessage() != null)
                error = "发生错误：" + e.getMessage();
            else
                error = "发生错误，无法登录。";
        }*/

        validateRequiredString("user.name", "nameMsg", "请输入用户名");
        validateRequiredString("user.pass", "passMsg", "请输入密码");
    }

    protected void handleError(Controller c) {
        c.keepModel(User.class);
        c.render("/jsp/login.jsp");
    }
}
